﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace Utils.Common
{
   
    public class ImageHelper  
    { 
        public ImageHelper()
        { }
 
        public static string ToBase64String(System.Drawing.Image img, ImageFormat format)
        {
            if (img != null)
            {
                using (var ms = new MemoryStream())
                {
                    img.Save(ms, format);
                    byte[] buffer = ms.ToArray();
                    return Convert.ToBase64String(buffer);
                }
            }
            return string.Empty;
        }

        public static System.Drawing.Image FromBase64String(string base64Str)
        {
            System.Drawing.Image img = null;
            using (var ms = new MemoryStream())
            {
                try
                {
                    var buffer = Convert.FromBase64String(base64Str);
                    ms.Write(buffer, 0, buffer.Length);
                    img = System.Drawing.Image.FromStream(ms);
                }
                catch
                {
                    //ignored
                }
            }
            return img;
        }
    }
}